<?php

namespace app\admin\controller\dormitory;

use app\admin\model\DormitoryBedExchange;
use app\admin\model\DormitoryDorm;
use app\admin\model\DormitoryDormBed;
use app\common\controller\Backend;
use app\common\library\Auth;
use app\common\model\BehaviorRecord;
use app\common\model\BehaviorRecordImage;
use think\Db;

class Image extends Backend
{
    /**
     * @var \app\admin\model\DormitoryDorm
     */
    protected $model = null;
    protected $searchFields = '';
    protected $noNeedRight = ['detail'];

    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('DormitoryDorm');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = BehaviorRecord::alias('a')
                ->join('__BEHAVIOR_RECORD_IMAGE__ b', 'b.record_id = a.id', 'left')
                ->where($where)
                ->count();

            $list = BehaviorRecord::alias('a')
                ->join('__BEHAVIOR_RECORD_IMAGE__ b', 'b.record_id = a.id', 'right')
                ->join('__DORMITORY_GARDEN__ d', 'd.number = a.garden_number', 'left')
                ->join('__DORMITORY_BUILDING__ e', 'e.number = a.building_number and e.garden_number = a.garden_number', 'left')
                ->where($where)
                ->field('b.id,a.record_user_id,a.record_user_group,a.created_at,a.dorm_number,a.bed_number,a.student_id,b.url,d.name as garden_name,e.name as building_name')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    public function detail($ids = null)
    {
        $this->assignconfig("ids", $ids);

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {

            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $dorm = DormitoryDorm::get($ids);

            if (!$dorm) $this->error('寝室不存在');

            $total = BehaviorRecord::alias('a')
                ->join('__BEHAVIOR_RECORD_IMAGE__ b', 'b.record_id = a.id', 'left')
                ->where('a.garden_number', $dorm['garden_number'])
                ->where('a.building_number', $dorm['building_number'])
                ->where('a.dorm_number', $dorm['number'])
                ->where($where)
                ->count();

            $list = BehaviorRecord::alias('a')
                ->join('__BEHAVIOR_RECORD_IMAGE__ b', 'b.record_id = a.id', 'right')
                ->join('__DORMITORY_GARDEN__ d', 'd.number = a.garden_number', 'left')
                ->join('__DORMITORY_BUILDING__ e', 'e.number = a.building_number and e.garden_number = a.garden_number', 'left')
                ->where('a.garden_number', $dorm['garden_number'])
                ->where('a.building_number', $dorm['building_number'])
                ->where('a.dorm_number', $dorm['number'])
                ->where($where)
                ->field('b.id,a.record_user_id,a.record_user_group,a.created_at,a.dorm_number,a.bed_number,a.student_id,b.url,d.name as garden_name,e.name as building_name')
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $ids = explode(',', $ids);
            $ids = array_unique($ids);

            $count = BehaviorRecordImage::where('id', 'in', $ids)->delete();
            if ($count) {
                $this->success();
            }
        }
        $this->error();
    }

}
